package com.neulogistics.contrib.transfercenter.api.application.converter;

import cn.hutool.json.JSONUtil;

import com.neulogistics.contrib.transfercenter.api.application.viewmodel.TransferOrderViewModel;
import com.neulogistics.contrib.transfercenter.domain.model.valueobject.OrderItem;
import com.neulogistics.contrib.transfercenter.infrastructure.persistence.orm.po.TransferOrderPO;
import org.springframework.beans.BeanUtils;

public class TransferOrderPOVMConverter {

    public static TransferOrderViewModel POToVM(TransferOrderPO transferOrderPO){
        if (transferOrderPO == null)
            return null;

        TransferOrderViewModel transferOrderViewModel = new TransferOrderViewModel();
        BeanUtils.copyProperties(transferOrderPO, transferOrderViewModel);
        // long转string
        transferOrderViewModel.setTransferOrderId(transferOrderPO.getTransferOrderId().toString());
        transferOrderViewModel.setOrderId(transferOrderPO.getOrderId().toString());
        if(transferOrderPO.getTaskId() != null){
            transferOrderViewModel.setTaskId(transferOrderPO.getTaskId().toString());
        }

        // json对象转换
        transferOrderViewModel.setOrderItems(JSONUtil.toList(transferOrderPO.getOrderItems(), OrderItem.class));

        return transferOrderViewModel;
    }
}
